﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMayTinhLinQ.Models;

namespace MvcMayTinhLinQ.Controllers
{
    public class ThuongHieuController : Controller
    {
        //
        // GET: /ThuongHieu/
        MayTinhDataDataContext db = new MayTinhDataDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AjaxHandler(PageInfo param)
        {
            var query = from th in db.thuonghieus
                        select th;
            var allThuongHieu = query.ToList();
            IEnumerable<thuonghieu> thuongHieuFiltered;
            //
            if (!string.IsNullOrEmpty(param.sSearch))
            {

                //kiem tra kha nang cot co the search duoc khong
                var isIdSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
                var isTenThuongHieuSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
                var isMoTaSearchable = Convert.ToBoolean(Request["bSearchable_3"]);
                thuongHieuFiltered = allThuongHieu.Where(th => isIdSearchable && Convert.ToString(th.Id).Contains(param.sSearch)
                                                               ||
                                                               isTenThuongHieuSearchable && th.TenThuongHieu.ToLower().Contains(param.sSearch.ToLower())
                                                               ||
                                                               isMoTaSearchable && th.MoTa.ToLower().Contains(param.sSearch.ToLower()));

            }
            else
            {
                thuongHieuFiltered = allThuongHieu;
            }
            var isIdSortable = Convert.ToBoolean(Request["bSortable_1"]);
            var isTenThuongHieuSortable = Convert.ToBoolean(Request["bSortable_2"]);
            var isMoTaSortable = Convert.ToBoolean(Request["bSortable_3"]);
            var sortCloumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
            Func<thuonghieu, string> orderFunction =
                (th => sortCloumnIndex == 1 && isIdSortable ? Convert.ToString(th.Id) :
                    sortCloumnIndex == 2 && isTenThuongHieuSortable
                                             ? th.TenThuongHieu
                                             : sortCloumnIndex == 3 && isMoTaSortable ? th.MoTa : "");
            var sortDirection = Request["sSortDir_0"];
            if (sortDirection == "asc")
            {
                thuongHieuFiltered = thuongHieuFiltered.OrderBy(orderFunction);
            }
            else
            {
                thuongHieuFiltered = thuongHieuFiltered.OrderByDescending(orderFunction);
            }
            var displayThuongHieu = thuongHieuFiltered.Skip(param.iDisplayStart).Take(param.iDisplayLength);
            var result = from th in displayThuongHieu select new[] { Convert.ToString(th.Id), th.TenThuongHieu, th.MoTa, th.Logo };
            return Json(new
                            {
                                sEcho = param.sEcho,
                                iTotalRecords = allThuongHieu.Count(),
                                iTotalDisplayRecords = thuongHieuFiltered.Count(),
                                aaData = result
                            }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /ThuongHieu/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ThuongHieu/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ThuongHieu/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ThuongHieu/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /ThuongHieu/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ThuongHieu/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ThuongHieu/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
